//
//  YFProfileTagVC.m
//  project
//
//  Created by Winny Zhang on 2019/4/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFProfileTagVC.h"
#import "YFTagModel.h"
#import "YFTagView.h"

@interface YFProfileTagVC ()

/**
 布局TableView
 */
@property (nonatomic, strong, nonnull) UITableView *tableView;

/**
 标签选择 View
 */
@property (nonatomic, strong) YFTagView *tagView;

@end

@implementation YFProfileTagVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"添加标签";
    
    [self setupViews];
    [self loadTagList];
    
}

- (void)setupViews {
    [self setupNavigationRightButton];
    [self setupTableView];
    [self setupTagView];
}

- (void)setupNavigationRightButton {
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(0, 0, 60, 44);
    button.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:16];
    [button setTitleColor:[UIColor colorWithHex:0x000000] forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    [button setTitle:@"保存" forState:(UIControlStateNormal)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    @weakify(self)
    [[button rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.tagView.selectedTags.count == 0) {
            [self showErrorHUDWithError:@"请选择标签"];
            return;
        }
        NSMutableArray * tags = [NSMutableArray arrayWithCapacity:self.tagView.selectedTags.count];
        for (YFUserInfoTagModel * tagModel in self.tagView.selectedTags) {
            NSDictionary * dic = @{
                                   @"name":tagModel.name,
                                   @"tagId":tagModel.tagId,
                                   @"color":@"#000000"
                                   };
            [tags addObject:dic];
        }
        [HJUserManager updateUserModelWithParams:@{@"tags":[tags yy_modelToJSONString]} success:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
//        if (self.selectedCompletion) {
//            self.selectedCompletion(self.tagView.selectedTags.count != 0, self.tagView.selectedTags);
//        }
    }];
}

- (void)setupTableView {
    UITableView *tableView = [[UITableView alloc] initWithFrame:SCREEN_BOUNDS style:(UITableViewStylePlain)];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.tableFooterView = [UIView new];
    [self.view insertSubview:tableView atIndex:0];
    self.tableView = tableView;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    if (IS_FULL_SCREEN) {
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 34, 0);
    } else {
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    self.tableView.scrollIndicatorInsets = _tableView.contentInset;
    
    self.tableView.tableHeaderView = self.tagView;
}

- (void)setupTagView {
    
    @weakify(self)
    self.tagView.updateContentViewFrameAction = ^(CGFloat height) {
        @strongify(self)
        [self.tableView beginUpdates];
        [UIView animateWithDuration:0.25 animations:^{
            UIView *header = self.tableView.tableHeaderView;
            [header setNeedsLayout];
            [header layoutIfNeeded];
            CGRect hFrame = header.frame;
            hFrame.size.height = height;
            header.frame = hFrame;
            self.tableView.tableHeaderView = header;
        }];
        [self.tableView endUpdates];
    };
}

#pragma mark - 网络请求
- (void)loadTagList {
    
    NSString *URLString = [Api stringByAppendingString:UserTagApi];
    
    [YFHud showProgress:@"加载中"];
    
    @weakify(self)
    [HJHttpTool get:URLString params:nil success:^(HJResponeds *result) {
        @strongify(self)
        if (result.isSuccess) {
            [YFHud dismissAnimated];
            YFTagModel *model = [YFTagModel yy_modelWithJSON:[result yy_modelToJSONString]];
            LKLog(@"用户标签列表%@", model);
            self.tagView.tagModel = model;
            
            if (self.defaultTags && self.defaultTags.count > 0) {
                self.tagView.defaultTags = self.defaultTags;
            }
        } else {
            [YFHud showError:result.resultDesc];
        }
    } failure:^(id result) {
        [YFHud showError:result];
    }];
}

#pragma mark Lazy Load
- (YFTagView *)tagView {
    if (!_tagView) {
        YFTagView *tagView = [YFTagView loadFromXib:[YFTagView class]];
        CGFloat height = 500;
        CGFloat safeArea = STATUS_BAR_HEIGHT == 44 ? 34 : 0;
        if (height < SCREEN_HEIGTH - Height_NavBar - safeArea) {
            height = SCREEN_HEIGTH - Height_NavBar - safeArea;
        }
        tagView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
        _tagView = tagView;
    }
    return _tagView;
}

@end
